package com.dongdongshop.controller;


import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.Result;
import com.dongdongshop.service.AddressService;
import com.dongdongshop.uitl.JwtUtil;
import com.dongdongshop.uitl.LoginUser;
import com.dongdongshop.vo.AddressVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("address")
public class AddressController {

    @Autowired
    private AddressService as;

    @Autowired
    private JwtUtil jwtUtil;

    //根据用户名获取收获地址
    @PostMapping("getAddress")
    public Result getAddress(@RequestHeader String AutharizeToken){
        Claims claims = jwtUtil.parseJwt(AutharizeToken);
        LoginUser loginUser = JSONObject.parseObject(claims.getSubject(), LoginUser.class);
        List<AddressVo > listvo = as.getAddress(loginUser.getUserName());
        return Result.OK().setData(listvo);
    }


    //增加地址
    @PostMapping("addAddress")
    public Result addAddress(@RequestHeader String AutharizeToken, @RequestBody AddressVo vo){
        Claims claims = jwtUtil.parseJwt(AutharizeToken);
        LoginUser loginUser = JSONObject.parseObject(claims.getSubject(), LoginUser.class);
        as.addAddress(loginUser.getUserName(), vo);
        return Result.OK();
    }

    //根据用户Id获取收获地址
    @PostMapping("getAddressById")
    public Result getAddressById(@RequestParam Long arrderssId){
        AddressVo listvo = as.getAddressById(arrderssId);
        return Result.OK().setData(listvo);
    }

}
